package cn.wilson.hrm.web.controller;

import cn.wilson.hrm.doc.CourseDoc;
import cn.wilson.hrm.query.CourseQuery;
import cn.wilson.hrm.repository.CourseElasticsearchRepository;
import cn.wilson.hrm.service.EsService;
import cn.wilson.hrm.utils.AjaxResult;
import cn.wilson.hrm.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/es")
public class EsController {

    @Autowired
    private CourseElasticsearchRepository courseElasticsearchRepository;

    @Autowired
    private EsService esService;


    /**
     *      添加到es中
     *       @RequestBody: 他在从前台传输的过程中会自动地将数据转换为CourseDoc对象
     * @param courseDoc
     * @return
     */
    @RequestMapping("/save")
    public AjaxResult save(@RequestBody CourseDoc courseDoc){
        courseElasticsearchRepository.save(courseDoc);
        return AjaxResult.me();
    }

    /**
     *      下线
     * @param courseDoc
     * @return
     */
    @RequestMapping("/offline")
    public AjaxResult offline(@RequestBody CourseDoc courseDoc){
        try {
            courseElasticsearchRepository.deleteById(courseDoc.getId());
            return AjaxResult.me().setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false);
        }
    }


    /**
     *      课程检索
     * @param courseQuery
     * @return
     */
    @RequestMapping("/searchCourse")
    public PageList<CourseDoc> searchCourse(@RequestBody CourseQuery courseQuery){
        return esService.searchCourse(courseQuery);
    }

}
